<?
	require_once(__DIR__ . '/../../models/Accounts.php');
	require_once(__DIR__ . '/../../inc/functions.php');
	global $rootUrl;
	$results = array();
	/* set the return URL (for Login requests) */
	if(isset($_REQUEST['returnUrl']))
		$_SESSION['returnUrl'] = $_REQUEST['returnUrl'];
	if(!isset($_SESSION['returnUrl']) && isset($_SERVER['HTTP_REFERER']))
		$_SESSION['returnUrl'] = $_SERVER['HTTP_REFERER'];
	
	/* determine which request */
	if(isset($_GET['logout'])) // Request is Log out
	{
		DoLogout(); 
		header("Location: $rootUrl/../W/Home"); // go home
		die();
	}	
	elseif(isset($_POST['email'])) // user submitted log in request
	{
		$row = $_POST;
		$results = DoLogin($_POST['email'], $_POST['password']);
		if($results === true)
		{
			$returnUrl = isset($_SESSION['returnUrl']) ? $_SESSION['returnUrl'] : "$rootUrl/../W/Accounts/login";
			if (!strstr($returnUrl,'?'))
				$returnUrl=$returnUrl . '?loggedin';
			else
				$returnUrl=$returnUrl . '&loggedin';
			header("Location: $returnUrl"); // go back to return URL
			die();
		}
	}else{ // no request submitted (show user the login page) 
		$row = array('email'=>null, 'password'=>null);
	}
?>
            	<? if(!empty($results)): /* Show Logon Error(s) */ ?>
			    <div class="alert alert-error">
				    <button type="button" class="close">×</button>
					<dl class="dl-horizontal error">
					<? foreach ($results as $key => $value) { ?>
						<dt><?=$key?></dt>
						<dd><?=$value?></dd>
					<? } ?>						
					</dl>
				</div>		
				<? endif; ?>
                <form class="form-horizontal" method="post" action="<?=$rootUrl?>/../W/Accounts/login">
                    <div class=" control-group">
                        <label class="control-label" for="email">Email:</label>
                        <div class="controls">
                            <input 	type="text" name="email" id="email" value="<?=$row['email']?>"
                            		 placeholder="yourEmailAddress" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="password">Password:</label>
                        <div class="controls">
                            <input 	type="password" name="password" id="password" value="<?=$row['password']?>" />
                        </div>   
                   </div>
                    <div class="control-group">
                    	<div class="controls">
                    		<input class="btn btn-primary" type="submit" value="Login" />
                        </div>
                    </div>
                </form>
            <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
			<script type="text/javascript">
			$(function(){
				/* enforce that email and password are required */
				$("form").validate({
					rules: 
					{
						email: { required: true} 
						,password: { required: true} 
					}
					,messages: 
					{
 					 	email: { required: "email is a required field" }
 					 	,password: {required: "password is a required field" }
					}
				});
				
				/* "close" the nearest alert boxes */
				$(".close").click(function(){
					$(this).closest(".alert").slideUp();
				});
			}); // end of document ready
			</script>

